<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="8">
            <a-form-model-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xm">
              <a-input v-model="model.xm" placeholder="请输入姓名"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol"  prop="sfzh">
              <a-input v-model="model.sfzh" placeholder="请输入身份证号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xb"> 
              <j-dict-select-tag
                type="list"
                v-model="model.xb"
                dictCode="xb"
                placeholder="请选择性别"
              />
            </a-form-model-item>
          </a-col>
          
          <a-col :span="8">
            <a-form-model-item label="出生年月" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="csny"> 
              <j-date placeholder="请选择出生年月" v-model="model.csny"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="民族" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mz"> 
              <j-dict-select-tag
                type="list"
                v-model="model.mz"
                dictCode="mz"
                placeholder="请选择民族"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lxdh">
              <a-input v-model="model.lxdh" placeholder="请输入联系电话"  ></a-input>
            </a-form-model-item>
          </a-col> 
          <a-col :span="8">
            <a-form-model-item label="入伍时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rwsj">
              <j-date placeholder="请选择入伍时间" v-model="model.rwsj"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="退伍时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="twsj">
              <j-date placeholder="请选择退伍时间" v-model="model.twsj"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="政治面貌" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zzmm"> 
              <j-dict-select-tag
                type="list"
                v-model="model.zzmm"
                dictCode="zzmm"
                placeholder="请选择政治面貌"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="入党时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rdsj">
              <j-date placeholder="请选择入党时间" v-model="model.rdsj"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          
          <a-col :span="8">
            <a-form-model-item label="健康状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jkzt"> 
              <j-dict-select-tag
                type="list"
                v-model="model.jkzt"
                dictCode="jkzk"
                placeholder="请选择健康状态"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="残疾性质" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cjxz"> 
              <j-dict-select-tag
                type="list"
                v-model="model.cjxz"
                dictCode="cjxz"
                placeholder="请选择残疾性质"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="入伍地" :labelCol="onelabelCol" :wrapperCol="onewrapperCol" prop="rwd">
              <!-- <j-select-depart v-model="model.rwd1" placeholder="请选择入伍地" /> -->
              <a-input v-model="model.rwd" placeholder="请输入入伍地"  ></a-input> 
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="拟安置地" :labelCol="onelabelCol" :wrapperCol="onewrapperCol" prop="nazd">
              <j-select-depart v-model="model.nazd1"  placeholder="请选择拟安置地" />
              <a-input v-model="model.nazd" placeholder="请输入拟安置地"  ></a-input>              
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="部队代号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bddh">
              <a-input v-model="model.bddh" placeholder="请输入部队代号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="退伍证号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="twzh">
              <a-input v-model="model.twzh" placeholder="请输入退伍证号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="安置方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="azfs"> 
              <j-dict-select-tag
                type="list"
                v-model="model.azfs"
                dictCode="azfs"
                placeholder="请选择安置方式"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="军龄" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jl">
              <a-input v-model="model.jl" placeholder="请输入军龄"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="入伍高校名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rwgxmc">
              <a-input v-model="model.rwgxmc" placeholder="请输入入伍高校名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="是否复学" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sffx"> 
              <j-dict-select-tag
                type="list"
                v-model="model.sffx"
                dictCode="is_open"
                placeholder="请选择是否复学"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="学历情况" :labelCol="onelabelCol" :wrapperCol="onewrapperCol" prop="xlqk">   
              <a-input v-model="model.xlqk" placeholder="输入或选择学历情况"  ></a-input> 
              <a-tag color="#108ee9" style="cursor:pointer" @click="addxlqk('高中')">高中</a-tag>
              <a-tag color="#108ee9" style="cursor:pointer" @click="addxlqk('中专')">中专</a-tag>
              <a-tag color="#108ee9" style="cursor:pointer" @click="addxlqk('大专')">大专</a-tag>
              <a-tag color="#108ee9" style="cursor:pointer" @click="addxlqk('大学本科')">大学本科</a-tag>
              <a-tag color="#108ee9" style="cursor:pointer" @click="addxlqk('研究生')">研究生</a-tag> 
            </a-form-model-item>
          </a-col>
          
          <a-col :span="24">
            <a-form-model-item label="奖惩信息" :labelCol="onelabelCol" :wrapperCol="onewrapperCol" prop="jcxx">   
              <a-input v-model="model.jlxx" placeholder="输入或选择奖惩信息"  ></a-input>
              <a-tag color="#108ee9" style="cursor:pointer"  @click="addjcxx('嘉奖')">嘉奖</a-tag>
              <a-tag color="#108ee9" style="cursor:pointer"  @click="addjcxx('一等功')">一等功</a-tag>
              <a-tag color="#108ee9" style="cursor:pointer"  @click="addjcxx('二等功')">二等功</a-tag>
              <a-tag color="#108ee9" style="cursor:pointer"  @click="addjcxx('三等功')">三等功</a-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="档案目录" :labelCol="onelabelCol" :wrapperCol="onewrapperCol" prop="mlmb">   
           
                <a-select v-model="model.mlmb"     >  
                   <a-select-option v-for="(item, key) in mldata" :key="key" :value="item.mbmc"> {{item.mbmc}} </a-select-option>
                </a-select>
            
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util' 
  export default {
    name: 'DaRyxxForm',
    components: { 
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      const idCardValidity = (rule, code, callback) => {
          var city = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江 ", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北 ", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏 ", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外 " };
          var tip = ""
          var pass = true
          if (!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)) {
            tip = "身份证号格式错误"
            pass = false;
          } else if (!city[code.substr(0, 2)]) {
            // tip = "地址编码错误"
            tip="身份证输入错误"
            pass = false
          } else {
            // 18位身份证需要验证最后一位校验位
            
            if (code.length === 18) { 
              
             
              code = code.split('')
              // ∑(ai×Wi)(mod 11)
              // 加权因子
              var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
              // 校验位
              var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2]
              var sum = 0
              var ai = 0
              var wi = 0
              for (var i = 0; i < 17; i++) {
                ai = code[i]
                wi = factor[i]
                sum += ai * wi
              }
              var last = parity[sum % 11];
              if (parity[sum % 11] != code[17]) {
                // tip = "校验位错误"
                tip="身份证输入错误"
                pass = false
              }
              if (pass){
                let code = this.model.sfzh;
                if (code.length === 18) { 
                      
                      let sex = null; 
                      let birth = null;
                      
                      sex = code.substring(16,17);
                      
                      if(sex%2 === 0)
                        this.model.xb = "2";
                      else
                        this.model.xb = "1";
                  
                      console.log("长度",this.model.xb);
                      
                      this.model.csny = code.substring(6,10)+"-"+code.substring(10,12)+"-"+code.substring(12,14);
                
                }
              }
            }
          }
          if (!pass) {
            callback(new Error(tip))
          } else {
            
            callback()
          }
      }
        // 设置手机号的验证规则
      const phoneRules = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请输入联系方式'))
        } else {
          const reg = /^1[0-9][0-9]\d{8}$/
          if (reg.test(value)) {
            callback()
          } else {
            return callback(new Error('请输入正确的电话'))
          }
        }
      }
      return {
        mldata:null,
        model:{
          xm:"",
          sfzh:"",
          xb:"",
          csny:"",
          mz:"",
          lxdh:"",
          rwsj:"",
          twsj:"",
          zzmm:"",
          rdsj:"",
          jkzt:"",
          cjxz:"",
          rwd1:"",
          rwd:"",
          nazd1:"",
          nazd:"",
          bddh:"",
          twzh:"",
          azfs:"",
          jl:"",
          rwgxmc:"",
          sffx:"",
          xlqk:"",
          xlqk1:"",
          jlxx:"", 
          mlmb:"",
        },
        onelabelCol: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        onewrapperCol: {
          xs: { span: 24 },
          sm: { span: 21 },
        },
        twolabelCol: {
          xs: { span: 4 },
          sm: { span: 5 },
        },
        twowrapperCol: {
          xs: { span: 24 },
          sm: { span: 19 },
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 9 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 },
        },
        confirmLoading: false,
        validatorRules: {
          sfzh: [
                {required: true, message: '请输入身份证', trigger: 'blur'},
                {
                  pattern: /(^\d{8}(0\d|10|11|12)([0-2]\d|30|31)\d{3}$)|(^\d{6}(18|19|20)\d{2}(0\d|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$)/,
                  message: '请输入正确的证件号', 
                  trigger: 'blur',
                  validator: idCardValidity
                }
              ],
          lxdh:[
                {required: true, message: '请输入联系电话',validator:phoneRules, trigger: 'blur'},
          ],
          xm: [
                {required: true, message: '请输入姓名', trigger: 'blur'}
              ],
          rwsj: [
                {required: true, message: '请选择入伍时间', trigger: 'blur'}
              ],
          twsj: [
                {required: true, message: '请选择退伍时间', trigger: 'blur'}
              ],
          mlmb: [
                {required: true, message: '请选择档案目录', trigger: 'blur'}
              ],

        },
        url: {
          add: "/dary/daRyxx/add",
          edit: "/dary/daRyxx/edit",
          queryById: "/dary/daRyxx/queryById",
          daml: "ap/apInfo/daml"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      
      httpAction(this.url.daml,'','post').then((res)=>{ 
           console.log(res);
           this.mldata = res.data;
           if (this.mldata.length > 0){
            this.model.mlmb = this.mldata[0].mbmc;
           }
          }).finally(() => { 
        }) 
    },
    methods: {
      addxlqk(val){ 
        if (this.model.xlqk == null){
          this.model.xlqk = val;
        }else{
          this.model.xlqk += val;
        }        
      },
      addjcxx(val){
        if (this.model.jlxx == null){
          this.model.jlxx = val;
        }else{
          this.model.jlxx += val;
        }       
      },
      add () { 
        this.edit(this.modelDefault);
        
        // this.model.xb = "2";   
      },
      edit (record) {
        
        this.model = Object.assign({}, record);
        this.model.rwgxmc = "绥化学院";     
        this.visible = true;
      },
      submitForm () {
        const that = this; 
        if (this.model.mlmb == "" || this.model.mlmb == null){
          that.$message.warning("请选择档案目录！");
          return;
        }
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            console.log(this.model);
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>